package com.iflytek.uoamp.agent.core.sender.monitorsender.requestObj;

import com.iflytek.uoamp.agent.common.util.ReplaceSpecialStr;
import com.iflytek.uoamp.agent.core.sender.monitorsender.monitorinterface.IItem;
import com.iflytek.uoamp.agent.core.sender.monitorsender.responseObj.BMonitorItem;

import org.apache.log4j.Logger;

import java.io.UnsupportedEncodingException;
import java.nio.ByteBuffer;
import java.util.*;

public class MonitorItemConfig {

	private String component_type;
	private String host;
	private int callFrequency;
	private int maxCheckAttempts;
	private String serverDesc;
	private String serverUrl;
	private String xml;
	private String method; // emue
	private String uptime;
	private String serviceFlag;
	private int limitResponseTime;
	private int limitRequestTime;
	private Set<String> whiteList;
	private String waringLimit;
	private String criticalLimit;
	private String monitoredIP;
	private String commandPath;
	private String retryinterval;
	private int hostObjectId;
	private int servicesObjectId;
	private Map<String, String> header = new HashMap<String, String>();
	public String getRetryinterval() {
		return retryinterval;
	}

	public void setRetryinterval(String retryinterval) {
		this.retryinterval = retryinterval;
	}

	public Set<String> getWhiteList() {
		if (whiteList == null)
			return new HashSet<String>();
		return whiteList;
	}

	public void setWhiteList(String whiteList) {
		this.whiteList = setToWhiteList(whiteList);
	}

	/**
	 * convert MonitorItemConfig to byte for thrift.
	 * */
	public ByteBuffer toByte() {
		// return ByteBuffer.wrap(getByte());
		try {
			return ByteBuffer.wrap(toString().getBytes("utf-8"));
		} catch (UnsupportedEncodingException e) {
			Logger.getRootLogger().error("MonitorItemConfig can't get bytes by utf-8",e);
			return ByteBuffer.allocate(0);
		}
	}

	private StringBuffer resultStrBuf = null;

	public String toString() {
		// if(resultStrBuf!=null) return resultStrBuf.toString();
		resultStrBuf = new StringBuffer();
		resultStrBuf.append("component_type" + (char) 30);
		resultStrBuf.append(ReplaceSpecialStr.replaceSpecialStr(component_type) + (char) 31);
		resultStrBuf.append("host" + (char) 30);
		resultStrBuf.append(ReplaceSpecialStr.replaceSpecialStr(host) + (char) 31);
		resultStrBuf.append("call_frequency" + (char) 30);
		resultStrBuf.append(callFrequency + "" + (char) 31);
		resultStrBuf.append("max_check_attempts" + (char) 30);
		resultStrBuf.append(maxCheckAttempts + "" + (char) 31);
		resultStrBuf.append("limit_response_time" + (char) 30);
		resultStrBuf.append(limitResponseTime + "" + (char) 31);
		resultStrBuf.append("limit_request_time" + (char) 30);
		resultStrBuf.append(limitRequestTime + "" + (char) 31);
		resultStrBuf.append("services_object_id" + (char) 30);
		resultStrBuf.append(servicesObjectId + "" + (char) 31);
		resultStrBuf.append("host_object_id" + (char) 30);
		resultStrBuf.append(hostObjectId + "" + (char) 31);
		resultStrBuf.append("server_desc" + (char) 30);
		resultStrBuf.append(ReplaceSpecialStr.replaceSpecialStr(serverDesc) + (char) 31);
		resultStrBuf.append("server_url" + (char) 30);
		resultStrBuf.append(ReplaceSpecialStr.replaceSpecialStr(serverUrl) + (char) 31);
		resultStrBuf.append("xml" + (char) 30);
		resultStrBuf.append(ReplaceSpecialStr.replaceSpecialStr(xml) + (char) 31);
		resultStrBuf.append("method" + (char) 30);
		resultStrBuf.append(method + (char) 31);
		resultStrBuf.append("service_flag" + (char) 30);
		resultStrBuf.append(ReplaceSpecialStr.replaceSpecialStr(serviceFlag) + (char) 31);
		resultStrBuf.append("white_list" + (char) 30);
		resultStrBuf.append(ReplaceSpecialStr.replaceSpecialStr(whiteListToString()) + (char) 31);
		resultStrBuf.append("waringLimit" + (char) 30);
		resultStrBuf.append(ReplaceSpecialStr.replaceSpecialStr(waringLimit) + (char) 31);
		resultStrBuf.append("criticalLimit" + (char) 30);
		resultStrBuf.append(ReplaceSpecialStr.replaceSpecialStr(criticalLimit) + (char) 31);
		resultStrBuf.append("monitoredIP" + (char) 30);
		resultStrBuf.append(ReplaceSpecialStr.replaceSpecialStr(monitoredIP) + (char) 31);
		resultStrBuf.append("commandPath" + (char) 30);
		resultStrBuf.append(ReplaceSpecialStr.replaceSpecialStr(commandPath) + (char) 31);
		resultStrBuf.append("retryinterval" + (char)30);
		resultStrBuf.append(ReplaceSpecialStr.replaceSpecialStr(retryinterval) + (char) 31);
		resultStrBuf.append("uptime" + (char) 30);
		resultStrBuf.append(ReplaceSpecialStr.replaceSpecialStr(uptime) + (char) 31 + "\n");
		return resultStrBuf.toString();
	}

	/**
	 * convert byte to MonitorItemConfig for use.
	 * */
	public static MonitorItemConfig getFromString(String byteObj) {
		MonitorItemConfig result = new MonitorItemConfig();
//		String str = null;
//		try {
//			str = getString(byteObj);
//		} catch (Exception e1) {
//			Logger.getRootLogger().error("MonitorItemConfig bytes can't change to string by utf-8",e1);
//			return new MonitorItemConfig();
//		}
		String[] kvs = byteObj.split((char) 31 + "");
		for (String cur : kvs) {
			String[] kv = cur.split((char) 30 + "");
			try {
				if (kv[0].equals("component_type")) {
					result.component_type = kv.length == 2 ? kv[1] : null;
				} else if (kv[0].equals("host")) {
					result.host = kv.length == 2 ? kv[1] : null;
				} else if (kv[0].equals("call_frequency")) {
					result.callFrequency = Integer.parseInt(kv.length == 2 ? kv[1] : null);
				} else if (kv[0].equals("max_check_attempts")) {
					result.maxCheckAttempts = Integer.parseInt(kv.length == 2 ? kv[1] : null);
				} else if (kv[0].equals("limit_response_time")) {
					result.limitResponseTime = Integer.parseInt(kv.length == 2 ? kv[1] : null);
				} else if (kv[0].equals("limit_request_time")) {
					result.limitRequestTime = Integer.parseInt(kv.length == 2 ? kv[1] : null);
				} else if (kv[0].equals("services_object_id")) {
					result.servicesObjectId = Integer.parseInt(kv.length == 2 ? kv[1] : null);
				} else if (kv[0].equals("host_object_id")) {
					result.hostObjectId = Integer.parseInt(kv.length == 2 ? kv[1] : null);
				} else if (kv[0].equals("server_desc")) {
					result.serverDesc = kv.length == 2 ? kv[1] : null;
				} else if (kv[0].equals("server_url")) {
					result.serverUrl = kv.length == 2 ? kv[1] : null;
				} else if (kv[0].equals("xml")) {
					result.xml = (kv.length == 2 ? kv[1] : null);
				} else if (kv[0].equals("method")) {
					result.method = kv.length == 2 ? kv[1] : null;
				} else if (kv[0].equals("uptime")) {
					result.uptime = kv.length == 2 ? kv[1] : null;
				} else if (kv[0].equals("service_flag")) {
					result.serviceFlag = kv.length == 2 ? kv[1] : null;
				} else if (kv[0].equals("waringLimit")) {
					result.waringLimit = kv.length == 2 ? kv[1] : null;
				} else if (kv[0].equals("criticalLimit")) {
					result.criticalLimit = kv.length == 2 ? kv[1] : null;
				} else if (kv[0].equals("white_list")) {
					result.whiteList = result.setToWhiteList(kv.length == 2 ? kv[1] : null);
				} else if (kv[0].equals("monitoredIP")) {
					result.monitoredIP = kv.length == 2 ? kv[1] : null;
				} else if (kv[0].equals("commandPath")) {
					result.commandPath = kv.length == 2 ? kv[1] : null;
				} else if (kv[0].equals("retryinterval")) {
					result.retryinterval = kv.length == 2 ? kv[1] : null;
				}
			} catch (Exception e) {
				Logger.getRootLogger().error("MonitorItemConfig " + kv + " get value error!",e);
			}
		}
		return result;
	}

	/**
	 * change white set to string,for thrift
	 * */
	private String whiteListToString() {
		if (whiteList == null)
			return "";
		StringBuffer temp = new StringBuffer();
		for (String cur : whiteList) {
			temp.append(cur + ",");
		}
		return temp.toString();
	}

	/**
	 * change white set to string,for thrift
	 * */
	private Set<String> setToWhiteList(String str) {
		if (str == null)
			return new HashSet<String>();
		String[] sets = str.split(",");
		Set<String> temp = new HashSet<String>();
		for (String cur : sets) {
			temp.add(cur);
		}
		return temp;
	}

	/**
	 * just change object
	 * */
	public BMonitorItem changeMonitorItemConfig2BMonitorItem() {
		BMonitorItem result = new BMonitorItem();
		result.setBody("");
		result.setHeaders(new HashMap<String, String>());
		return result;
	}

	public String getComponent_type() {
		return component_type;
	}

	public void setComponent_type(String component_type) {
		this.component_type = component_type;
	}

	public String getHost() {
		return host;
	}

	public void setHost(String host) {
		this.host = host;
	}

	public int getCallFrequency() {
		return callFrequency;
	}

	public void setCallFrequency(int callFrequency) {
		this.callFrequency = callFrequency;
	}

	public String getServerDesc() {
		return serverDesc;
	}

	public void setServerDesc(String serverDesc) {
		this.serverDesc = serverDesc;
	}

	public String getServerUrl() {
		return serverUrl;
	}

	public void setServerUrl(String serverUrl) {
		this.serverUrl = serverUrl;
	}

	public String getXml() {
		return xml;
	}

	public void setXml(String xml) {
		this.xml = xml;
	}

	public String getMethod() {
		return method;
	}

	public void setMethod(String method) {
		this.method = method;
	}

	public String getUptime() {
		return uptime;
	}

	public void setUptime(String uptime) {
		this.uptime = uptime;
	}

	public String getServiceFlag() {
		return serviceFlag;
	}

	public void setServiceFlag(String serviceFlag) {
		this.serviceFlag = serviceFlag;
	}

	public int getLimitResponseTime() {
		return limitResponseTime;
	}

	public void setLimitResponseTime(int limitResponseTime) {
		this.limitResponseTime = limitResponseTime;
	}

	public int getLimitRequestTime() {
		return limitRequestTime;
	}

	public void setLimitRequestTime(int limitRequestTime) {
		this.limitRequestTime = limitRequestTime;
	}

	public String getWaringLimit() {
		return waringLimit;
	}

	public void setWaringLimit(String waringLimit) {
		this.waringLimit = waringLimit;
	}

	public String getCriticalLimit() {
		return criticalLimit;
	}

	public void setCriticalLimit(String criticalLimit) {
		this.criticalLimit = criticalLimit;
	}

	public String getMonitoredIP() {
		return monitoredIP;
	}

	public void setMonitoredIP(String monitoredIP) {
		this.monitoredIP = monitoredIP;
	}

	public String getCommandPath() {
		return commandPath;
	}

	public void setCommandPath(String commandPath) {
		this.commandPath = commandPath;
	}

	public StringBuffer getResultStrBuf() {
		return resultStrBuf;
	}

	public void setResultStrBuf(StringBuffer resultStrBuf) {
		this.resultStrBuf = resultStrBuf;
	}

	public void setWhiteList(Set<String> whiteList) {
		this.whiteList = whiteList;
	}

	public Map<String, String> getHeader() {
		return header;
	}

	public void setHeader(Map<String, String> header) {
		this.header = header;
	}

	public Integer getNextRetryInterval(Integer curInterval) {
		if(retryinterval==null){
			return curInterval;
		}
		String[] retryArray = retryinterval.split(",");
		if (retryArray.length > 0) {
			List<Integer> retryList = retryListToSortList(retryArray);
			
			for (Integer retryValue : retryList) {
				if (retryValue < curInterval ) {
					return retryValue;
				}
			}
			
			return curInterval;
		} else {
			return curInterval;
		}
	}
	
	private List<Integer> retryListToSortList(String[] retryArray) {
		List<Integer> frequencyList = new ArrayList<Integer>();
	    for (Object obj : retryArray)
	    {
	    	frequencyList.add(Integer.valueOf((String)obj));
	    }
		
		Collections.sort(frequencyList, Collections.reverseOrder());
		return frequencyList;
	}

	public int getMaxCheckAttempts() {
		return maxCheckAttempts;
	}

	public void setMaxCheckAttempts(int maxCheckAttempts) {
		this.maxCheckAttempts = maxCheckAttempts;
	}

	public int getHostObjectId() {
		return hostObjectId;
	}

	public void setHostObjectId(int hostObjectId) {
		this.hostObjectId = hostObjectId;
	}

	public int getServicesObjectId() {
		return servicesObjectId;
	}

	public void setServicesObjectId(int servicesObjectId) {
		this.servicesObjectId = servicesObjectId;
	}

}
